package com.djq.hhc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djq.hhc.dto.PostTypeDto;
import com.djq.hhc.model.post.PostInfo;
import com.djq.hhc.model.post.PostType;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.PostTypeService;
import com.djq.hhc.vo.PostInfoVo;
import com.djq.hhc.vo.PostTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/02/下午 9:04
 * @Description:
 */

@RestController
@RequestMapping(value = "/post/postType")
public class PostTypeController {

    @Autowired
    private PostTypeService postTypeService;

    //新增分类
    @PostMapping("/save")
    public Result save(@RequestBody PostType postType) {
        return postTypeService.saveType(postType);
    }

    //返回所有分类, 不包含时间
    @GetMapping("/getAllType")
    public Result getAllType() {
        List<PostType> postTypeList = postTypeService.list();
        List<PostTypeDto> res = postTypeList.stream()
                .map(postType -> new PostTypeDto(postType.getId(), postType.getTypeName()))
                .collect(Collectors.toList());
        return Result.ok(res);
    }

    //删除分类
    @DeleteMapping("/remove/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean b = postTypeService.removeById(id);
        if (b) {
            return Result.ok().message("删除成功!");
        } else {
            return Result.fail().message("删除失败");
        }
    }

    //修改分类
    @PutMapping("/update")
    public Result updateType(@RequestBody PostType postType) {
        return postTypeService.updateType(postType);
    }

    //获取类别
    @GetMapping("/get/{id}")
    public Result getTypeById(@PathVariable Integer id) {
        PostType postType = postTypeService.getById(id);
        return Result.ok(postType);
    }

    //分页
    @PostMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) PostTypeVo postTypeVo) {
        //创建page对象，传递当前页和每页记录数
        Page<PostType> pageParam = new Page<>(page, limit);
        //获取条件值
        Long id = postTypeVo.getId();
        String typeName = postTypeVo.getTypeName();
        //封装条件
        QueryWrapper<PostType> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(id)) {
            wrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(typeName)) {
            wrapper.like("type_name",typeName);
        }
        //调用方法得到分页查询结果
        IPage<PostType> pageModel = postTypeService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }
}
